<?php
set_time_limit(1800);

require_once 'vendor/autoload.php';
require_once 'db.php';
require_once 'config.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()
    ->setHosts([SEARCH_URL])
    ->build();

// 删除索引
/*$params = [
    'index' => 'pattern'
];
$client->indices()->delete($params);
exit;*/

// 创建索引
/*$params = [
    'index' => 'pattern',
    'body' => [
        'settings' => [
            'number_of_shards' => 3,
            'number_of_replicas' => 2
        ],
        'mappings' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'pid' => [
                    'type' => 'integer',
                ],
                'is_public' => [
                    'type' => 'integer',
                ],
                'content' => [
                    'type' => "text"
                ],
                'no' => [
                    'type' => 'keyword'
                ],
                'picture' => [
                    'type' => 'keyword'
                ],
                'picture_thumb' => [
                    'type' => 'keyword'
                ],
                'weight' => [
                    'type' => 'double'
                ],
                'height' => [
                    'type' => 'double'
                ],
                'widthheight' => [
                    'type' => 'double'
                ],
                'widthlow' => [
                    'type' => 'double'
                ],
                "series" => [
                    'type' => 'keyword'
                ],
                'sealing' => [
                    'type' => 'text'
                ],
                'level' => [
                    'type' => 'keyword'
                ]
            ]
        ]
    ]
];

$client->indices()->create($params);
exit;*/

// 添加文档
/*$items = $database->select("pattern", "*");
foreach ($items as $pattern) {
    $properties = $database->select("pattern_property", "*", [
        'pid' => $pattern['pid']
    ]);

    $pattern['properties'] = $properties;

    $data = array(
        'pid' => $pattern['pid'],
        'is_public' => $pattern['is_public'],
        'content' => '',
        'picture' => $pattern['picture'],
        'picture_thumb' => $pattern['picture_thumb'],
        'weight' => $pattern['weight'],
        'height' => $pattern['height'],
        'widthheight' => $pattern['widthheight'],
        'widthlow' => $pattern['widthlow'],
        'no' => $pattern['no'],
        'component' => $pattern['component'],
        'series' => $pattern['series'],
        'level' => $pattern['level']
    );

    // 处理封板================= begin =========================
    $plates_out = [];
    $plates = json_decode($pattern['plates'], true);

    foreach ($plates as $plate) {
        if (count($plate) == 1) {
            $plates_out[] = $plate[0] . $plate[0];
        } else {
            $plates_out[] = implode(',', $plate);
        }
    }

    if (count($plates_out) > 0) {
//        $data['sealing'] = implode("|", $plates_out);
        $data['sealing'] = $plates_out;
    } else {
        $data['sealing'] = [];
    }
    // 处理封板=================== end ===========================

    // 设置属性
    $properties = $pattern['properties'];
    $property_strings = '';

    // 属性字段拼接
    if ($properties != null) {
        foreach ($properties as $property) {
            $json = json_decode($property['property_value'], true);
            $value = $json['value'];

            if (is_array($value)) {
                $property_strings .= ' ' . implode(',', $value);
            } else {
                $property_strings .= ' ' . $value;
            }
        }
    }

    $data['content'] .= $pattern['no'] . $property_strings;


    $param = [
        'index' => 'pattern',
        'id' => $pattern['no'],
        'body' => $data
    ];

    try {
        $client->index($param);
        echo $pattern['no'] . '<br />';
    } catch (Exception $exception) {
        print_r($exception);
    }
}
exit;*/

/*
$rt = Array(
    "HZ5759N",
    "HZ5953N",
    "HZ6217N",
    "HX5442N2",
    "HX6155N",
    "HZ5578J",
    "HZ5288Y",
    "HZ5579NC",
    "HX5699N",
    "HX5367N1",
    "HZ6012N",
    "HT5859N",
    "HZ5972N",
    "HZ6052NR1",
    "HZ6078N",
    "HZ6101N",
    "HZ5180NR2",
    "HZ5843N",
    "HZ6282N",
    "HZ5578N",
    "HY5475N",
    "HX6145N",
    "HZ5956N",
    "HZ5931J",
    "HZ5719N",
    "HZ5967N",
    "HZ5923N1",
    "HX6137N",
    "HZ2013NR",
    "HZ5645N",
    "HZ5180NR",
    "HX5630N",
    "HZ5618N",
    "HX5772N",
    "HZ5175N",
    "HZ5740N",
    "HX5568N",
    "HZ5583NV",
    "HZ5821N",
    "HZ5585N",
    "HZ4887N1",
    "HZ5515N",
    "HZ4243N1",
    "HZ6317N",
    "HZ5507N",
    "HZ5579N",
    "HX6210N"
);

$param = [
    'index' => 'pattern',
    'body'=>[
        'query'=> [
            'bool' => [
                'must' => [
                    ['match_all' => new stdClass()]
//                    [
//                        'ids' => ['values' => $rt]
//                    ],
//                    [
//                        'match_phrase' => ['content' => '浪漫甜美'],
//                    ],
//                    [
//                        'range' => [
//                            'weight' => [
//                                'lt' => 40
//                            ]
//                        ]
//                    ],
//                    [
//                        'range' => [
//                            'height' => [
//                                'gt' => 10
//                            ]
//                        ]
//                    ],
//                    [
//                        'bool' => [
//                            'should' => [
//                                [
//                                    'match' => ['content' => '单色'],
//                                ],
//                                [
//                                    'match' => ['content' => '双色'],
//                                ],
//                            ]
//                        ]
//                    ]
//                    [
//                        'bool' => [
//                            'should' => [
//                                [
//                                    'match_phrase' => ['content' => '直边'],
//                                ],
//                                [
//                                    'match_phrase' => ['content' => '双波'],
//                                ],
//                            ]
//                        ]
//                    ]
                ],

            ],

        ]
    ],
    'from' => 0,
    'size' => 20
];

$result = $client->search($param);
print_r($result);
*/